﻿public class Solution {
    public int MonotoneIncreasingDigits(int n)
    {
        char[] numCharArray = n.ToString().ToCharArray();

        int flag = numCharArray.Length;
        for (int i = numCharArray.Length - 1; i > 0; i--)
        {
            if (numCharArray[i] < numCharArray[i - 1])
            {
                flag = i;
                numCharArray[i - 1] = (char)(numCharArray[i - 1] - 1);
            }
        }

        for (int i = flag; i < numCharArray.Length; i++) numCharArray[i] = '9';
        return int.Parse(new string(numCharArray));
    }

    public static void Main(string[] args)
    {
        int n = 1234;

        Solution solution = new Solution();
        int res = solution.MonotoneIncreasingDigits(n);
        Console.WriteLine(res);
    }
}